interface paramsProps {
  min?: number
  max?: number
  splitNumber?: number
  data: any[]
}
interface result {
  min: number | null
  max: number | null
  interval: number | null
}

function computedArrayRange(data: any[]) {
  const list: any[] = []
  data.forEach((item) => {
    if (typeof item === 'object' && item !== null) {
      list.push(item.value || 0)
    } else {
      list.push(item || 0)
    }
  })
  list.sort((a, b) => a - b)
  if (list[0] < 0) {
    return {
      min: null,
      max: null
    }
  }
  return {
    min: Math.floor(list[0]),
    max: Math.ceil(list[list.length - 1])
  }
}

function handleMaxNum(num: number, multiple: number, splitNumber: number) {
  const prevIndex = multiple - 1
  if (num > prevIndex * splitNumber && num <= multiple * splitNumber) {
    num = multiple * splitNumber
  }
  return num
}

function getMaxNum(num: string) {
  if (!num) {
    return ''
  }
  const len = num.length
  let ynum = 0
  if (len >= 1) {
    ynum = Number(num.substring(0, 1))
    ynum++
    const newLen = len - 1
    for (let i = 0; i < newLen; i++) {
      ynum = Number(`${ynum}0`)
    }
  } else {
    ynum = 10
  }
  return ynum
}

function getMinNum(num: string) {
  if (!num) {
    return ''
  }
  const len = num.length
  let ynum = 0
  if (len >= 1) {
    ynum = Number(num.substring(0, 1))
    const newLen = len - 1
    for (let i = 0; i < newLen; i++) {
      ynum = Number(`${ynum}0`)
    }
  } else {
    ynum = 10
  }
  return ynum
}

function computedAxisValue(min: number, max: number, splitNumber: number): result {
  let agentMin = Number(getMinNum(`${min}`))
  if (`${agentMin}`.length < 3) {
    agentMin = 0
  }
  let agentMax
  if (max >= 10) {
    agentMax = Number(getMaxNum(`${max}`))
  } else {
    agentMax = max
  }
  let originInterval = (agentMax - agentMin) / splitNumber
  let interval: number | null = null
  // 最大值为三位数以上
  if (`${Math.ceil(agentMax)}`.length >= 3) {
    if (originInterval % 100 !== 0) {
      // 原始间隔
      originInterval = Math.ceil(originInterval)
      // 将间隔规范化
      interval = Number(getMaxNum(`${originInterval}`))
      // 最大值
      agentMax = Number(agentMin) + interval * splitNumber
    } else {
      interval = originInterval
    }
  } else {
    // 最大值为100以下的数
    if (agentMax > 0 && agentMax <= 40) {
      const multiple = Math.ceil(agentMax / splitNumber)
      agentMax = handleMaxNum(agentMax, multiple, splitNumber)
      interval = multiple
    }
    if (agentMax > 40 && agentMax <= 60) {
      agentMax = 60
      interval = 15
    } else if (agentMax > 60 && agentMax <= 80) {
      agentMax = 80
      interval = 20
    } else if (agentMax > 80) {
      agentMax = 100
      interval = 25
    }
  }
  return {
    min: Number(agentMin),
    max: Number(agentMax),
    interval
  }
}

export default function computedIntervalData({ min, max, splitNumber = 4, data = [] }: paramsProps): result {
  let numObj: result = {
    min: null,
    max: null,
    interval: null
  }
  if (data.length) {
    const originNumObj = computedArrayRange(data)
    if (originNumObj.min === null && originNumObj.max === null) {
      numObj = {
        ...originNumObj,
        interval: null
      }
    } else {
      numObj = computedAxisValue(originNumObj.min, originNumObj.max, splitNumber)
    }
  } else {
    numObj = computedAxisValue(Math.floor(min), Math.ceil(max), splitNumber)
  }
  return numObj
}
